<?php

namespace app\index\controller;
use think\Controller;
use think\Config;
use think\Db;

/**
 *	商家Controller
 */
class Shop extends Controller
{
	const SHOP_NUM_PER_PAGE = 5;

	const MAX_FOODS_COUNT = 6;

	// 是否是商家
	public function isBusiness() {
		$openid = $_POST['openid'];
		// $openid = $_GET['openid'];
		$user = model('User');
		$res = $user->where('openid', $openid)->find();
		if ($res) {
			$arr = array(
				'openid' => $res->openid,
				'shop_id' => $res->shop_id,
			);
		} else {
			$arr = array('errmsg' => 'Not find User!');
		}
		echo json_encode($arr);
		exit();
	}

	// 上传商户信息
	public function uploadShopInfo() {
		$info = $_POST;
		$shop = model('Shop');
		$user = model('User');
		$openid = $info['openid'];
		$res = $shop->where('openid', $openid)->find();
		$arr = array('code' => 0);
		if ($res) {

			$shop->where('id', $res->id)
				->update([
					'shop_name' => $info['shop_name'],
					'shop_style' => $info['shop_style'],
					'location' => $info['location'],
					'open_time' => $info['open_time'],
					'end_time' => $info['end_time'],
					'latitude' => $info['latitude'],
					'longitude' => $info['longitude'],
					'tel' => $info['tel'],
					'notice' => $info['notice'],
					'logo_url' => $info['logo_url'],
					'region' => $info['region'],
					'region_index_0' => $info['region_index_0'],
					'region_index_1' => $info['region_index_1'],
					'area' => $info['area']
				]);

			$arr = array(
					'code' => 1,
					'shop_id' => $res->id,
					'info' => '商户更新成功'
				);

		} else {

			$this->addShopStyle($info['shop_style']);

			$shop->data([
				'openid' => $info['openid'],
				'shop_name' => $info['shop_name'],
				'shop_style' => $info['shop_style'],
				'location' => $info['location'],
				'open_time' => $info['open_time'],
				'end_time' => $info['end_time'],
				'latitude' => $info['latitude'],
				'longitude' => $info['longitude'],
				'tel' => $info['tel'],
				'notice' => $info['notice'],
				'logo_url' => $info['logo_url'],
				'region' => $info['region'],
				'region_index_0' => $info['region_index_0'],
				'region_index_1' => $info['region_index_1'],
				'area' => $info['area'],
				'max_foods_count' => self::MAX_FOODS_COUNT,
			]);


			if ($shop->save() && $user->save(['shop_id' => $shop->id], ['openid' => $info['openid']])) {
				model('AuthCode')->where('code', $info['auth_code'])->update(['is_use' => 1]);
				$arr = array(
					'code' => 1,
					'shop_id' => $shop->id,
					'info' => '商户创建成功'
				);
			}
		}
		echo json_encode($arr);
		exit();
	}

	// 添加商户类别
	protected function addShopStyle($shopStyle) {
		$shopStyles = model('ShopStyles');
		if (!($shopStyles->where('shop_style', $shopStyle)->find())) {
			$shopStyles->data([
				'shop_style' => $shopStyle
			]);
			$shopStyles->save();
		}
	}


	// 获取商户信息
	public function getShopInfo() {
		$shopId = $_POST['shop_id'];
		$openid = $_POST['openid'];
		$shop = model('Shop')->where('id', $shopId)->find();
		$res = array(
			'code' => 0 
		);
		if ($shop) {
			$res = array(
				'code' => 1,
				'openid' => $shop->openid,
				'shop_name' => $shop->shop_name,
				'shop_style' => $shop->shop_style,
				'location' => $shop->location,
				'open_time' => $shop->open_time,
				'end_time' => $shop->end_time,
				'latitude' => $shop->latitude,
				'longitude' => $shop->longitude,
				'tel' => $shop->tel,
				'collect_num' => $shop->collect_num,
				'notice' => $shop->notice,
				'logo_url' => $shop->logo_url,
				'region' => $shop->region,
				'region_index_0' => $shop->region_index_0,
				'region_index_1' => $shop->region_index_1,
				'area' => $shop->area,
				'max_foods_count' => $shop->max_foods_count,
			);
			$res['is_collect'] = model('CollectShop')->where(['shop_id' => $shopId, 'openid' => $openid])->find() ? 1 : 0;
		}
		echo json_encode($res);
		exit();
	}


	public function getShops() {
		$latitude = $_POST['latitude'];
		$longitude = $_POST['longitude'];

		$page = $_POST['page'];
		$openid = $_POST['openid'];
		// $orderRule = $_POST['order_rule'];
		$start = ($page - 1) * self::SHOP_NUM_PER_PAGE;
		$perPageNum = self::SHOP_NUM_PER_PAGE;

		$area = $_POST['area'];
		$region = $_POST['region'];
		$shopStyle = $_POST['shop_style'];
		$orderRule = $_POST['order_rule'];

		$shopStyleCondition = strlen($shopStyle) > 0 ? "WHERE shop_style = '{$shopStyle}'" : "";

		$regionStr = strlen($region) > 0 ? "region = '{$region}'" : (strlen($area) > 0 ? "area = '{$area}'" : "");

		$regionCondition = strlen($regionStr) > 0 ? (strlen($shopStyleCondition) > 0 ? "AND {$regionStr}" : "WHERE {$regionStr}") : "";

		$condition = $shopStyleCondition . " " . $regionCondition;

		$order = $orderRule == 'distance' ? 'asc' : 'desc';

		$sql = "SELECT *, ( 6371 * acos( cos( radians('{$latitude}') ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians('{$longitude}') ) + sin( radians('{$latitude}') ) * sin( radians( latitude ) ) ) ) AS distance FROM shop {$condition} ORDER BY {$orderRule} {$order} LIMIT {$start}, {$perPageNum}";

		// echo $sql;
		// exit();

		$res = Db::query($sql);

		for ($i = 0, $count = count($res); $i < $count; $i++) {
			$shopId = $res[$i]['id'];
			$foodNum = model('Food')->where('shop_id', $shopId)->count();
			$res[$i]['food_num'] = $foodNum;
			$res[$i]['is_collect'] = model('CollectShop')->where(['shop_id' => $shopId, 'openid' => $openid])->find() ? 1 : 0;
		}

		echo json_encode($res);
		exit();

	}

	// 获取所有商户类别
	public function getShopStyles() {
		$res = array('code' => 0);
		$shopStyles = model('ShopStyles');
		$data = $shopStyles -> select();
		if (count($data) > 0) {
			$res['code'] = 1;
			$res['data'] = $data;
		}
		echo json_encode($res);
		exit();
	}
	
}